//
//  SearchTitleView.m
//  富学宝典3
//
//  Created by xiaohui on 2016/11/4.
//  Copyright © 2016年 xiaohui. All rights reserved.
//

#import "SearchTitleView.h"
#import "WBSearchBar.h"
#import "SDAutoLayout.h"

@interface SearchTitleView()<UITextFieldDelegate>

@property(nonatomic,weak)UIButton*cancelBtn;



@property(nonatomic,strong)NSString*textFieldString;

@end

@implementation SearchTitleView
@synthesize searchText = _searchText;

+ (instancetype)titleView{
    
    SearchTitleView  *titleView = [[self alloc]initWithFrame:CGRectMake(0,navHeight1, ScreenW,40)];
    titleView.backgroundColor =[UIColor whiteColor];
    
    return titleView;    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame: frame]) {
       
        // 创建子控件
        [self setUpChilds];
    }
    return self;
}

- (void)setUpChilds{
    
    UIButton*cancelBtn = [[UIButton alloc]init];
    [cancelBtn addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:@"取消" forState:0];
    cancelBtn.titleLabel.font =[UIFont systemFontOfSize:14];
    [cancelBtn setTitleColor:RGBColor(20, 133, 255) forState:UIControlStateNormal];
    [self addSubview:cancelBtn];
    _cancelBtn = cancelBtn;
    
    //创建搜索框
    WBSearchBar  *searchBar = [[WBSearchBar alloc]init]; 
    searchBar.placeholder = @"搜索";
    searchBar.delegate = self;
    searchBar.textAlignment = NSTextAlignmentCenter;
    [self addSubview:searchBar];
    _searchBar = searchBar;
    [searchBar addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [searchBar setSd_cornerRadius:@(5)];
    
    cancelBtn.sd_layout
    .topSpaceToView(self,3)
    .rightSpaceToView(self,15)
    .widthIs(40)
    .heightIs(self.xh_Height - 2*3);
    
    //创建搜索框
    searchBar.sd_layout
    .leftSpaceToView(self,20)
    .topSpaceToView(self,3)
    .rightSpaceToView(self,20)
    .heightIs(self.xh_Height - 2*3);
    
}

#pragma  mark - 按钮点击代理事件

- (void)backButtonClick{
    
    self.searchBar.text =nil;
    self.searchBar.textAlignment = NSTextAlignmentCenter;
    [self.searchBar resignFirstResponder];
    self.searchBar.sd_resetLayout
    .leftSpaceToView(self,20)
    .topSpaceToView(self,3)
    .rightSpaceToView(self,20)
    .heightIs(self.xh_Height - 2*3);
    
    if (_delegate &&[_delegate respondsToSelector:@selector(searchTileViewTextFieldDidChange:)])
    {
        [_delegate searchTileViewTextFieldDidChange:@""];
    }
}

- (void)searchItenClick
{

}

#pragma  mark - textfield的代理
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    
    return YES;
}

#pragma  mark - setter、getter方法

- (void)setSearchText:(NSString *)searchText{
    _searchText = searchText;
    self.searchBar.text = searchText;
//    [self.searchBar becomeFirstResponder];
}

-(void)setReturnKeyType:(UIReturnKeyType)returnKeyType
{
    _returnKeyType = returnKeyType;
    _searchBar.returnKeyType=_returnKeyType;
}

- (NSString *)searchText{
    
    return self.searchBar.text;
}

- (void)resignSearchBarResponder{
    
    [self.searchBar resignFirstResponder];
    [self backButtonClick];
}

-(void)dismiss
{
    [self.searchBar resignFirstResponder];
    self.searchBar.text =nil;
     self.searchBar.textAlignment = NSTextAlignmentCenter;
    [self.searchBar resignFirstResponder];
    self.searchBar.sd_resetLayout
    .leftSpaceToView(self,20)
    .topSpaceToView(self,3)
    .rightSpaceToView(self,20)
    .heightIs(self.xh_Height - 2*3);
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.searchBar.textAlignment = NSTextAlignmentLeft;
    self.searchBar.sd_resetLayout
    .leftSpaceToView(self,20)
    .topSpaceToView(self,3)
    .rightSpaceToView(self,55)
    .heightIs(self.xh_Height - 2*3);
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
//    if (textField.text.length==0) {
//
//        self.searchBar.textAlignment = NSTextAlignmentCenter;
//        self.searchBar.sd_resetLayout
//        .leftSpaceToView(self,10)
//        .topSpaceToView(self,3)
//        .rightSpaceToView(self,10)
//        .heightIs(self.xh_Height - 2*3);
//    }
   
}

-(void)textFieldDidChange:(UITextField *)textField
{
    if (![self.textFieldString isEqualToString:textField.text])
    {
        self.textFieldString =textField.text;
        if (_delegate &&[_delegate respondsToSelector:@selector(searchTileViewTextFieldDidChange:)])
        {
            [_delegate searchTileViewTextFieldDidChange:textField.text];
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_delegate &&[_delegate respondsToSelector:@selector(searchTileViewSearchButtonClickSearchString:)])
    {
        [_delegate searchTileViewSearchButtonClickSearchString:textField.text];
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)setTitlePlaceholder:(NSString *)str{
    self.searchBar.placeholder = str;
}
@end
